<html lang="en-US">

<head>
    
<title>使用K6对接口进行压测 - 花落雨忧</title>

<meta property="og:title" content="使用K6对接口进行压测 - 花落雨忧">



    



    
    <meta property="description" content="在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具
[&amp;hellip;] # 在windows命令行工具输入 winget install k6 # 安装完成后，查看版本 k6 --version k6 v0.45.0 (2023-06-19T08:41:01&#43;0000/v0.45.0-0-gc3b4587b, go1.20.5, windows/amd64) Step2：使用 # 在 &amp;hellip;">
    <meta property="og:description" content="在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具
[&amp;hellip;] # 在windows命令行工具输入 winget install k6 # 安装完成后，查看版本 k6 --version k6 v0.45.0 (2023-06-19T08:41:01&#43;0000/v0.45.0-0-gc3b4587b, go1.20.5, windows/amd64) Step2：使用 # 在 &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/%E4%BD%BF%E7%94%A8k6%E5%AF%B9%E6%8E%A5%E5%8F%A3%E5%8E%8B%E6%B5%8B/">使用K6对接口进行压测</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2024/08/06</a>
                                            
                                            
                                            
                                            
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <h3 id="使用k6对接口进行压测">使用K6对接口进行压测</h3>
<h3 id="背景">背景</h3>
<blockquote>
<p>在实际开发中，常常需要对接口进行性能测试，因此，少不了压测工具</p>
</blockquote>
<ul>
<li>在网上找了几款压测工具，感觉k6安装和使用都比较方便</li>
</ul>
<h3 id="step1安装">step1：安装</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">在windows命令行工具输入</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">winget</span> <span style="color:#a6e22e">install</span> <span style="color:#a6e22e">k6</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">安装完成后</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">查看版本</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">k6</span> <span style="color:#f92672">--</span><span style="color:#a6e22e">version</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">k6</span> <span style="color:#a6e22e">v0</span><span style="color:#ae81ff">.45.0</span> (<span style="color:#ae81ff">2023</span><span style="color:#f92672">-</span><span style="color:#ae81ff">06</span><span style="color:#f92672">-</span><span style="color:#ae81ff">19</span><span style="color:#a6e22e">T08</span>:<span style="color:#ae81ff">41</span>:<span style="color:#ae81ff">01</span><span style="color:#f92672">+</span><span style="color:#ae81ff">0000</span><span style="color:#f92672">/</span><span style="color:#a6e22e">v0</span><span style="color:#ae81ff">.45.0</span><span style="color:#f92672">-</span><span style="color:#ae81ff">0</span><span style="color:#f92672">-</span><span style="color:#a6e22e">gc3b4587b</span>, <span style="color:#a6e22e">go1</span><span style="color:#ae81ff">.20.5</span>, <span style="color:#a6e22e">windows</span><span style="color:#f92672">/</span><span style="color:#a6e22e">amd64</span>)
</span></span></code></pre></div><h3 id="step2使用">Step2：使用</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">在项目根目录下创建文件夹pressureTest</span><span style="color:#960050;background-color:#1e0010">【</span><span style="color:#a6e22e">自定义</span><span style="color:#960050;background-color:#1e0010">】</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">在该文件夹下创建js文件</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">文件内容为压测的内容</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">k6</span> <span style="color:#a6e22e">run</span> <span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">js</span>
</span></span></code></pre></div><h3 id="step3示例">Step3：示例</h3>
<blockquote>
<p>dataReceive.js</p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">import</span> <span style="color:#a6e22e">http</span> <span style="color:#a6e22e">from</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">k6</span><span style="color:#f92672">/</span><span style="color:#a6e22e">http</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> { <span style="color:#a6e22e">check</span>, <span style="color:#a6e22e">sleep</span>, <span style="color:#a6e22e">group</span> } <span style="color:#a6e22e">from</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">k6</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">export</span> <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">options</span> = {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">vus</span>: <span style="color:#ae81ff">500</span>, <span style="color:#75715e">// 虚拟用户数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>  <span style="color:#a6e22e">duration</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#ae81ff">30</span><span style="color:#a6e22e">s</span><span style="color:#960050;background-color:#1e0010">&#39;</span>, <span style="color:#75715e">// 压测持续时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">export</span> <span style="color:#66d9ef">default</span> <span style="color:#a6e22e">function</span> () {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">group</span>(<span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Example</span> <span style="color:#a6e22e">API</span><span style="color:#960050;background-color:#1e0010">&#39;</span>, <span style="color:#a6e22e">function</span> () {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 定义请求的payload数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">payload</span> = <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>({
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">sn</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#ae81ff">3500000200000001</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">timestamp</span>: <span style="color:#ae81ff">1719904084</span>,
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">data</span>: [
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_p0</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">上层温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.1</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">status</span>: {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p0_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">上层温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          },
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_p1</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">中层温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.2</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">status</span>: {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p1_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">中层温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          },
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_p2</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">下层温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.3</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">status</span>: {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p2_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">下次温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          },
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">temp_in</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">空气温度</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">unit</span>: <span style="color:#960050;background-color:#1e0010">&#39;°</span><span style="color:#a6e22e">C</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">20.6</span>,
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">status</span>: {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">p0_temp_status</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">name_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">空气温度传感器状态</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">value</span>: <span style="color:#ae81ff">0</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_en</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">normal</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">status_zh</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">正常</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>          },
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>      ],
</span></span><span style="display:flex;"><span>    });
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 发送POST请求
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">res</span> = <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">post</span>(<span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">https</span>:<span style="color:#75715e">//brewing-sass.nongbotech.com/api/device/data/receive&#39;, payload, {
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>      <span style="color:#a6e22e">headers</span>: {
</span></span><span style="display:flex;"><span>        <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Content</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Type</span><span style="color:#960050;background-color:#1e0010">&#39;</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">json</span><span style="color:#960050;background-color:#1e0010">&#39;</span>,
</span></span><span style="display:flex;"><span>      },
</span></span><span style="display:flex;"><span>    });
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 检查响应状态码
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">check</span>(<span style="color:#a6e22e">res</span>, {
</span></span><span style="display:flex;"><span>      <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">status</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200</span><span style="color:#960050;background-color:#1e0010">&#39;</span>: (<span style="color:#a6e22e">r</span>) =&gt; <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">status</span> <span style="color:#f92672">==</span>= <span style="color:#ae81ff">200</span>,
</span></span><span style="display:flex;"><span>    });
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 添加自定义指标
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">responseTime</span> = <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">timings</span>.<span style="color:#a6e22e">duration</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">apiName</span> = <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Example</span> <span style="color:#a6e22e">API</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">tags</span> = { <span style="color:#a6e22e">endpoint</span>: <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#f92672">/</span><span style="color:#a6e22e">device</span><span style="color:#f92672">/</span><span style="color:#a6e22e">data</span><span style="color:#f92672">/</span><span style="color:#a6e22e">receive</span><span style="color:#960050;background-color:#1e0010">&#39;</span> };
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">data</span> = {
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">responseTime</span>: <span style="color:#a6e22e">responseTime</span>,
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">sampleRate</span> = <span style="color:#ae81ff">0.5</span>; <span style="color:#75715e">// 采样率，用于控制报告中指标的采样数量
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">customMetrics</span> = {
</span></span><span style="display:flex;"><span>      [<span style="color:#a6e22e">apiName</span>]: {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">tags</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">data</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">sampleRate</span>,
</span></span><span style="display:flex;"><span>      },
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 将自定义指标发送到k6的输出
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#75715e">// 根据需要可以将指标发送到InfluxDB等其他目标
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">let</span> <span style="color:#a6e22e">customMetricPayload</span> = <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">customMetrics</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">customMetricPayload</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">sleep</span>(<span style="color:#ae81ff">1</span>); <span style="color:#75715e">// 每个虚拟用户执行完后等待1秒钟
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>  });
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="step4-查看报告">Step4: 查看报告</h3>
<blockquote>
<p>进入dataReceive.js目录，执行 k6 run dataReceive.js</p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span> <span style="color:#a6e22e">Example</span> <span style="color:#a6e22e">API</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>       <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#a6e22e">status</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>        <span style="color:#960050;background-color:#1e0010">↳</span>  <span style="color:#ae81ff">0</span><span style="color:#f92672">%</span> <span style="color:#960050;background-color:#1e0010">—</span> <span style="color:#960050;background-color:#1e0010">✓</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">/</span> <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#ae81ff">8513</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">checks</span><span style="color:#f92672">.....................</span>: <span style="color:#ae81ff">0.00</span><span style="color:#f92672">%</span>   <span style="color:#960050;background-color:#1e0010">✓</span> <span style="color:#ae81ff">0</span>          <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#ae81ff">8513</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">data_received</span><span style="color:#f92672">............</span>..: <span style="color:#ae81ff">4.4</span> <span style="color:#a6e22e">MB</span>  <span style="color:#ae81ff">96</span> <span style="color:#a6e22e">kB</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">data_sent</span><span style="color:#f92672">..................</span>: <span style="color:#ae81ff">9.2</span> <span style="color:#a6e22e">MB</span>  <span style="color:#ae81ff">203</span> <span style="color:#a6e22e">kB</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">group_duration</span><span style="color:#f92672">............</span>.: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">1.96</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">1.01</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">1.03</span><span style="color:#a6e22e">s</span>   <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">24.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">4.56</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">6.01</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_blocked</span><span style="color:#f92672">.........</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">194.11</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">19.29</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">450.07</span><span style="color:#a6e22e">ms</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_connecting</span><span style="color:#f92672">......</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">4.33</span><span style="color:#a6e22e">ms</span>   <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">5.46</span><span style="color:#a6e22e">s</span>  <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">47.26</span><span style="color:#a6e22e">ms</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_duration</span><span style="color:#f92672">.........</span>.: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">676.84</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">16.98</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">23.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">2.39</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">4.33</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_failed</span><span style="color:#f92672">............</span>: <span style="color:#ae81ff">100.00</span><span style="color:#f92672">%</span> <span style="color:#960050;background-color:#1e0010">✓</span> <span style="color:#ae81ff">8513</span>       <span style="color:#960050;background-color:#1e0010">✗</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_receiving</span><span style="color:#f92672">.........</span>: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">102.33</span><span style="color:#a6e22e">µs</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">4.53</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">504.29</span><span style="color:#a6e22e">µs</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">780.59</span><span style="color:#a6e22e">µs</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_sending</span><span style="color:#f92672">.........</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">27.07</span><span style="color:#a6e22e">µs</span>  <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">2.52</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">55.85</span><span style="color:#a6e22e">µs</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_tls_handshaking</span><span style="color:#f92672">...</span>: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">174.71</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">18.95</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>       <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">112.85</span><span style="color:#a6e22e">ms</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_req_waiting</span><span style="color:#f92672">.........</span>..: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">676.71</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">0</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">16.82</span><span style="color:#a6e22e">ms</span> <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">23.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">2.39</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">4.33</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">http_reqs</span><span style="color:#f92672">..................</span>: <span style="color:#ae81ff">8513</span>    <span style="color:#ae81ff">187.091907</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">iteration_duration</span><span style="color:#f92672">.........</span>: <span style="color:#a6e22e">avg</span>=<span style="color:#ae81ff">1.96</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">1.01</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">med</span>=<span style="color:#ae81ff">1.03</span><span style="color:#a6e22e">s</span>   <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">24.81</span><span style="color:#a6e22e">s</span> <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">90</span>)=<span style="color:#ae81ff">4.56</span><span style="color:#a6e22e">s</span>    <span style="color:#a6e22e">p</span>(<span style="color:#ae81ff">95</span>)=<span style="color:#ae81ff">6.01</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">iterations</span><span style="color:#f92672">...............</span>..: <span style="color:#ae81ff">8513</span>    <span style="color:#ae81ff">187.091907</span><span style="color:#f92672">/</span><span style="color:#a6e22e">s</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">vus</span><span style="color:#f92672">........................</span>: <span style="color:#ae81ff">1</span>       <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">1</span>        <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">500</span>
</span></span><span style="display:flex;"><span>     <span style="color:#a6e22e">vus_max</span><span style="color:#f92672">..................</span>..: <span style="color:#ae81ff">500</span>     <span style="color:#a6e22e">min</span>=<span style="color:#ae81ff">500</span>      <span style="color:#a6e22e">max</span>=<span style="color:#ae81ff">500</span>
</span></span></code></pre></div><p><img src="https://www.lican.site/images/1722937145315.png" alt="1722937145315"></p>


                                        
                                        
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.asia/tech/%E4%BD%BF%E7%94%A8k6%E5%AF%B9%E6%8E%A5%E5%8F%A3%E5%8E%8B%E6%B5%8B/"
                                                    rel="prev">使用K6对接口进行压测</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.asia/tech/%E6%90%AD%E5%BB%BAvpn/">搭建vpn</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>